package com.lagou.edu;

import java.io.*;

public class MyClassLoader extends ClassLoader{

    private String path;

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public MyClassLoader(String path){
        this.path=path;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        String url = path + name.replace(".","\\") + ".class";
        try {
            FileInputStream inputStream = new FileInputStream(new File(url));
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            int len = 0;
            byte[] buffer = new byte[1024];
            while((len = inputStream.read(buffer))!= -1){
                byteArrayOutputStream.write(buffer,0,len);
            }
            byteArrayOutputStream.flush();
            byte[] classByte = byteArrayOutputStream.toByteArray();
            return defineClass(name,classByte,0,classByte.length);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
